/*
 * NewCheckUpDialog.java
 *
 * Created on May 25, 2008, 4:48 PM
 */
package org.potomak.patientmanager.gui;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.border.Border;

import org.potomak.patientmanager.tools.Validation;

import org.potomak.patientmanager.beans.model.CheckUpBean;
import org.potomak.patientmanager.beans.model.PatientBean;
import org.potomak.patientmanager.persistence.JPoxJDO;

/**
 *
 * @author  mobile
 */
public class NewCheckUpDialog extends JDialog {

    private JPoxJDO jpoxJDO;
    private Border defaultBorder;

    /** Creates new form NewCheckUpDialog */
    public NewCheckUpDialog(JFrame parent, JPoxJDO jpoxJDO) {
        super(parent);

        this.jpoxJDO = jpoxJDO;

        setResizable(false);

        parent.setEnabled(false);
        parent.setFocusableWindowState(false);

        addWindowListener(new java.awt.event.WindowAdapter() {

            public void windowClosing(WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        initComponents();
        
        defaultBorder = dateField.getBorder();

        getRootPane().setDefaultButton(addCheckUpButton);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        patientInfoTab = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        dateLabel = new javax.swing.JLabel();
        weightLabel = new javax.swing.JLabel();
        weightField = new javax.swing.JTextField();
        dateField = new javax.swing.JTextField();
        addCheckUpButton = new javax.swing.JButton();
        cancelCheckUpButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New check up");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("CheckUp info"));

        dateLabel.setText("Date");

        weightLabel.setText("Weight (Kg)");

        weightField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                weightFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                weightFieldFocusLost(evt);
            }
        });
        weightField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                weightFieldKeyReleased(evt);
            }
        });

        dateField.setText((new SimpleDateFormat("dd/MM/yyyy")).format(new Date()));
        dateField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                dateFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                dateFieldFocusLost(evt);
            }
        });
        dateField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                dateFieldKeyReleased(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(weightLabel)
                    .add(dateLabel))
                .add(99, 99, 99)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(dateField)
                    .add(weightField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(weightLabel)
                    .add(weightField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(dateLabel)
                    .add(dateField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        addCheckUpButton.setText("Add");
        addCheckUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCheckUpButtonActionPerformed(evt);
            }
        });

        cancelCheckUpButton.setText("Cancel");
        cancelCheckUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelCheckUpButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout patientInfoTabLayout = new org.jdesktop.layout.GroupLayout(patientInfoTab);
        patientInfoTab.setLayout(patientInfoTabLayout);
        patientInfoTabLayout.setHorizontalGroup(
            patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(patientInfoTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(patientInfoTabLayout.createSequentialGroup()
                        .add(cancelCheckUpButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(addCheckUpButton))
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        patientInfoTabLayout.setVerticalGroup(
            patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(patientInfoTabLayout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(patientInfoTabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addCheckUpButton)
                    .add(cancelCheckUpButton))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(patientInfoTab, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(patientInfoTab, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(WindowEvent evt) {
        this.getParent().setEnabled(true);
        ((JFrame) this.getParent()).setFocusableWindowState(true);
        setVisible(false);
    }

private void addCheckUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCheckUpButtonActionPerformed

    PatientBean patient = ((Contacts) this.getParent()).getSelectedPatient();
    
    if(!Validation.validateWeight(weightField.getText())) {
        return;
    }
    
    Double weight = new Double(weightField.getText().trim());
    
    if(!Validation.validateDate(dateField.getText())) {
        return;
    }
    
    Date date = null;
    try {
        date = (new SimpleDateFormat("dd/MM/yyyy")).parse(dateField.getText());
    } catch (Exception e) {
        e.printStackTrace();
    }

    CheckUpBean checkUp = new CheckUpBean(patient, weight, date);

    boolean success = jpoxJDO.insertCheckUp(checkUp);

    if (!success) {
        JOptionPane.showMessageDialog(this, "Check up info not saved.", "Error", JOptionPane.ERROR_MESSAGE);
    }

    ((Contacts) this.getParent()).updateCheckUpsTableData();
    this.getParent().setEnabled(true);
    ((JFrame) this.getParent()).setFocusableWindowState(true);
    setVisible(false);
}//GEN-LAST:event_addCheckUpButtonActionPerformed

private void cancelCheckUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelCheckUpButtonActionPerformed
    this.getParent().setEnabled(true);
    ((JFrame) this.getParent()).setFocusableWindowState(true);
    setVisible(false);
}//GEN-LAST:event_cancelCheckUpButtonActionPerformed

private void validateWeight() {
    if(Validation.validateWeight(weightField.getText())) {
        weightField.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.green, 2));
    }
    else {
        weightField.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.red, 2));
    }
}

private void weightFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_weightFieldKeyReleased
    validateWeight();
}//GEN-LAST:event_weightFieldKeyReleased

private void weightFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_weightFieldFocusGained
    validateWeight();
}//GEN-LAST:event_weightFieldFocusGained

private void weightFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_weightFieldFocusLost
    if(Validation.validateWeight(weightField.getText())) {
        weightField.setBorder(defaultBorder);
    }
}//GEN-LAST:event_weightFieldFocusLost

private void validateDate() {
    if(Validation.validateDate(dateField.getText())) {
        dateField.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.green, 2));
    }
    else {
        dateField.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.red, 2));
    }
}

private void dateFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_dateFieldFocusGained
    validateDate();
}//GEN-LAST:event_dateFieldFocusGained

private void dateFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_dateFieldFocusLost
    if(Validation.validateDate(dateField.getText())) {
        dateField.setBorder(defaultBorder);
    }
}//GEN-LAST:event_dateFieldFocusLost

private void dateFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dateFieldKeyReleased
    validateDate();
}//GEN-LAST:event_dateFieldKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addCheckUpButton;
    private javax.swing.JButton cancelCheckUpButton;
    private javax.swing.JTextField dateField;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel patientInfoTab;
    private javax.swing.JTextField weightField;
    private javax.swing.JLabel weightLabel;
    // End of variables declaration//GEN-END:variables
}
